package config

import "fmt"

type Config struct {
	Database DatabaseConfig `yaml:"database"`
	Tables   TablesConfig   `yaml:"tables"`
	Server   ServerConfig   `yaml:"server"`
	TableName string       `yaml:"tableName"` // 添加表名字段
}

type ServerConfig struct {
	Port int `yaml:"port"`
}

type DatabaseConfig struct {
	Host      string `yaml:"host"`
	Port      int    `yaml:"port"`
	User      string `yaml:"user"`
	Password  string `yaml:"password"`
	DBName    string `yaml:"dbname"`
	Charset   string `yaml:"charset"`
	ParseTime bool   `yaml:"parseTime"`
}

type TablesConfig struct {
	Transactions TableConfig `yaml:"transactions"`
}

type TableConfig struct {
	Columns []ColumnConfig `yaml:"columns"`
}

type ColumnConfig struct {
	Name          string `yaml:"name"`
	Type          string `yaml:"type"`
	PrimaryKey    bool   `yaml:"primary_key,omitempty"`
	AutoIncrement bool   `yaml:"auto_increment,omitempty"`
	Nullable      bool   `yaml:"nullable,omitempty"`
	Default       string `yaml:"default,omitempty"`
}

func (dc *DatabaseConfig) GetDSN() string {
	return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&parseTime=%v",

		dc.User, dc.Password, dc.Host, dc.Port, dc.DBName, dc.Charset, dc.ParseTime)
}
